<?php

/**

 * @see Alinks_ImportExport_Import_Abstract

 */

require_once('Alinks/ImportExport/Import/Abstract.php');



/**

 * Utility class for importing keyphrases from ale files

 *

 * The "ale" format is an old export format that will no longer be

 * supported. It was basically a new-line delimited file, that was

 * then base64 encoded to keep users from messing with the contents.

 *

 * Copyright (C) 2008  Sean Hickey

 * 

 * This program is free software: you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation, either version 3 of the License, or

 * (at your option) any later version.

 *

 * This program is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: Ale.php 391 2008-07-03 05:42:56Z sean $

 * @package Alinks_ImportExport

 */

class Alinks_ImportExport_Import_Ale extends Alinks_ImportExport_Import_Abstract

{

	/**

	 * Imports an exported ale file

	 *

	 * @param string $ale The ale data

	 * @return bool

	 * @throws Alinks_ImportExport_Import_Exception

	 */

	public function import($ale)

	{

		$contents = explode("\n", $ale, 2);

		$import_hash = $contents[0];

		$hash_match = md5($contents[1]);

		if ($import_hash != $hash_match) {

			/**

			 * @see Alinks_ImportExport_Import_Exception

			 */

			require_once('Alinks/ImportExport/Import/Exception.php');

			throw new Alinks_ImportExport_Import_Exception('The ale file appears to be corrupt. The hashes do not match.');

		}

		

		$keyphrases = array();

		$factory_settings = array();

		$alinks_settings = array();

		

		$contents = base64_decode($contents[1]);

		$parts = explode('#%%#', $contents);

		for ($i = 0, $t = count($parts); $i < $t; $i++) {

			if (empty($parts[$i])) continue;

			if (strstr($parts[$i], 'KEYPHRASES')) {

				$keyphrases = $this->_getAleKeyphrases($parts[$i]);

			} else if (strstr($parts[$i], 'SETTINGS_') && $this->_options[self::IMPORT_FACTORY_SETTINGS]) {

				$factory_settings = array_merge($factory_settings, $this->_getAleModSettings($parts[$i]));

			} else if (strstr($parts[$i], 'SETTINGS#**#') && $this->_options[self::IMPORT_SETTINGS]) {

				$alinks_settings = $this->_getAleSettings($parts[$i]);

			}

		}

		

		$this->_setKeyphrases($keyphrases);

		if ($this->_options[self::IMPORT_SETTINGS]) {

			$this->_setSettings($alinks_settings);

		}

		if ($this->_options[self::IMPORT_FACTORY_SETTINGS]) {

			$this->_setFactorySettings($factory_settings);

		}

		

		return true;

	}

	

	/**

	 * Returns the keyphrases from an ale file

	 *

	 * @param string $data The keyphrases part of the ale file

	 * @return Alinks_Keyphrase[]

	 */

	protected function _getAleKeyphrases($data)

	{

		 /**

		  * #%%KEYPHRASES#***#

		  * book

		  * Get books at amazon.com

		  * a:2:{s:10:"searchterm";s:12:"harry potter";s:11:"producttype";s:5:"books";}

		  * amazon basic

		  * 2006-05-08 11:34:56

		  * adf4sdfbn43089adfgjasdfklj430ugdf

		  */

	 

		$s_data = explode("\n", $data, 2); // remove "#%%KEYPHRASES#***#"

		$s_data = trim($s_data[1]);

		$keyphrases = explode("\n", $s_data);

	

		$found = array();

		for ($i = 0; $i < count($keyphrases); $i += 6) {

			if ($keyphrases[$i + 3] == 'general') {

				$keyphrases[$i + 3] = 'default';

			} else if ($keyphrases[$i + 3] == 'amazon') {

				$keyphrases[$i + 3] = 'amazon_basaic';

			}

			

			$k = new Alinks_Keyphrase($keyphrases[$i + 3]);

			$k->setPhrase($keyphrases[$i]);

			$k->setDescription($keyphrases[$i + 1]);

			$k->setOptions(unserialize($keyphrases[$i + 2]));

			$k->setHash($keyphrases[$i + 5]);

			$k->setCreated($keyphrases[$i + 4]);

			$found[] = $k;

		}

		

		return $found;

	}

	

	/**

	 * Returns the factory settings found in an ale file

	 *

	 * @param string $data A factory setting part of the ale file

	 * @return array

	 */

	protected function _getAleModSettings($data)

	{

		/**

		 * #%%#SETTINGS_alinks_mod_amazon#**#

		 * a:4:{s:12:"setting_name";s:17:"alinks_mod_amazon";s:8:"amazonid";s:11:"myassoc-id";s:9:"amazonurl";s:12:"amazon.co.jp";}

		 */

	 

		$s_data = explode("\n", $data, 2);

		$name = str_replace('SETTINGS_alinks_mod_', '', $s_data[0]);

		$name = str_replace('#**#', '', $name);

		if ($name == 'amazon') {

			$name = 'amazon_basic';

		} else if ($name == 'general') {

			$name = 'default';

		}

		$s_data = trim($s_data[1]);

		return array($name => unserialize($s_data));

	}

	

	/**

	 * Returns the aLink settings from an ale file

	 *

	 * @param string $data The settings part of the ale file

	 * @return array

	 */

	protected function _getAleSettings($data)

	{

		/**

		 * #%%#SETTINGS#**#

		 * linkimage:http://www.mysite.com/link.gif

		 * targetwindow:_blank

		 * clicktracking:on

		 * customhtmljs:

		 * cssclass:alinks_link

		 */

	 

		$s_data = explode("\n", $data, 2);

		$s_data = trim($s_data[1]);

		

		$settings = explode("\n", $s_data);

		$alinks_settings = array();

		foreach ($settings as $setting) {

			$e_settings = explode(':', $setting, 2);

			

			/**

			 * The option names have changed since previous versions. Need

			 * to update.

			 */

			switch($e_settings[0]) {

				case 'targetwindow':

				case 'newwindow':

					$e_settings[0] = Alinks_Settings::NEW_WINDOW;

					if (empty($e_settings[1])) {

						$e_settings[1] = Alinks_Settings::DEFAULT_NEW_WINDOW;

					} else {

						$e_settings[1] = 1;

					}

					break;

				case 'customhtmljs':

					$e_settings[0] = Alinks_Settings::INNER_HTML;

					break;

				case 'cssclass':

					$e_settings[0] = Alinks_Settings::CLASSES;

					break;

			}

			$alinks_settings[$e_settings[0]] = $e_settings[1];

		}



		return $alinks_settings;

	}

}

